package com.gzhu.knowledgeAdmin.dao;

import com.gzhu.knowledgeAdmin.entity.Column;
import com.gzhu.knowledgeAdmin.entity.TableLabel;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface TableMapper {

    @Select("SHOW TABLES") // 使用合适的SQL语句来获取所有表名
    List<String> getAllTableNames();

    @Select("<script> SELECT * FROM ${tableName} </script>")
    List<Map<String, Object>> getTableData(@Param("tableName") String tableName);

    @Select("SELECT COLUMN_NAME as name, COLUMN_COMMENT as label, DATA_TYPE as type FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = #{tableName}")
    List<Column> getTableColumns(@Param("tableName") String tableName);

    @Select("<script> ${query} </script>")
    List<Map<String, Object>> executeSearchQuery(@Param("query") String query);

    @Insert("<script>"
            + "INSERT INTO ${tableName} "
            + "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"
            + "<foreach collection=\"data.keySet()\" item=\"key\" separator=\",\">"
            + "${key}"
            + "</foreach>"
            + "</trim>"
            + "VALUES "
            + "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"
            + "<foreach collection=\"data.values()\" item=\"value\" separator=\",\">"
            + "#{value}"
            + "</foreach>"
            + "</trim>"
            + "</script>")
    void insertDataIntoTable(@Param("tableName") String tableName, @Param("data") Map<String, Object> data);

    @Delete("<script>"
            + "DELETE FROM ${tableName} "
            + "WHERE f_id IN "
            + "<foreach item='item' collection='idsToDelete' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    int deleteRowsByIds(@Param("tableName") String tableName, @Param("idsToDelete") List<Integer> idsToDelete);

    @Update("<script>"
            + "UPDATE ${tableName} SET "
            + "<foreach collection=\"data\" index=\"key\" item=\"value\" separator=\",\">"
            + "${key} = #{data[${key}]}"
            + "</foreach>"
            + " WHERE f_id = #{F_ID}"
            + "</script>")
    int updateRecord(@Param("tableName") String tableName, @Param("F_ID") Integer F_ID, @Param("data") Map<String, Object> data);

    @Select("SELECT table_name, label FROM table_labels")
    List<TableLabel> findAllTableLabels();

    // 其他与数据库交互的方法
}
